package com.zzedu.system.service.impl;

import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.system.domain.ConfigI18n;
import com.zzedu.system.mapper.ConfigI18nMapper;
import com.zzedu.system.service.ISysConfigI18nService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 国际化配置数据库Service业务层处理
 *
 * @author Mr.Wang
 * @date 2023-08-15
 */
@Service
public class SysConfigI18NServiceImpl extends ServiceImpl<ConfigI18nMapper, ConfigI18n> implements ISysConfigI18nService {

    final String cacheName = CacheNameContansts.MESSAGE_CODE;

    @Override
    public Wrapper buildQuery(ConfigI18n configI18n) {
        LambdaQueryWrapper<ConfigI18n> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(configI18n.getLanguageKey()), ConfigI18n::getLanguageKey, configI18n.getLanguageKey());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(configI18n.getLanguageValue()), ConfigI18n::getLanguageValue, configI18n.getLanguageValue());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(configI18n.getLanguage()), ConfigI18n::getLanguage, configI18n.getLanguage());
        lambdaQueryWrapper.orderByDesc(ConfigI18n::getId);
        return lambdaQueryWrapper;
    }

    @Override
    @Cacheable(cacheNames = cacheName, key = "#language+'_'+#languageKey")
    public ConfigI18n getCodeAndLanguage(String languageKey, String language) {
        if(!StringUtils.isNotBlank(languageKey)||!StringUtils.isNotBlank(language)){
            return null;
        }
        return this.getOne(Wrappers.lambdaQuery(ConfigI18n.class).eq(ConfigI18n::getLanguageKey,languageKey).eq(ConfigI18n::getLanguage,language));
    }

    @Override
    @CacheEvict(cacheNames = cacheName, key = "#entity.language+'_'+#entity.languageKey")
    public boolean save(ConfigI18n entity) {
        return super.save(entity);
    }

    @Override
    @CacheEvict(cacheNames = cacheName, key = "#entity.language+'_'+#entity.languageKey")
    public boolean updateById(ConfigI18n entity) {
        return super.updateById(entity);
    }
    @Override
    @CacheEvict(cacheNames = cacheName, key = "#entity.language+'_'+#entity.languageKey")
    public void clearCache(ConfigI18n entity){

    }

}